package com.edu.springboot.controller;

import com.edu.springboot.domain.Users;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//springmvc
//x
//@RestController=@Controller+@ResponseBody
@RestController

@RequestMapping("/test")
//SpringBoot的跨域处理
@CrossOrigin(value = "*",maxAge = 3600)
//@CrossOrigin("*")
public class TestController {


    //读取配置文件中指定属性的值完成bean的属性注入
    @Value("${server.port}")
    private Integer port;

    // @Value("${server.swedu}")
    private  String edu;
    //    @ResponseBody
    @GetMapping("/demo1")
    public String demo1()
    {
        System.out.println("demo1.....端口号:"+this.port+"学院名称:"+this.edu);
        return "demo1.....端口号:"+this.port;
    }

    @GetMapping("/demo2")
    public String demo2()
    {
        return "demo2.....";
    }


    //http://localhost:8090/test/selectbyid?id=999
    @GetMapping("/selectbyid")
    public Users selectById(Integer id)
    {
        Users users=new Users(id,"admin","123abc",18);
        return  users;
    }


    //http://localhost:8090/test/selectbyid2/999
    //restful风格
    @GetMapping("/selectbyid2/{id}")
    public Users selectById2(@PathVariable("id") Integer id)
    {
        Users users=new Users(id,"admin","123abc",18);
        return  users;
    }

    //前端使用 AJAX 的 post 方式请求时 @RequestBody实现参数序列化
    //前端传来的json对象,json对象的key必须要与java对象的属性名称相同,值就直接赋给对象对应的属性.否是对象的属性值为null
    // 就直接转成java对象,否则对象的属性为null

    //@RequestBody 把json对象转成java对象
    @PostMapping("/insert")
    public  String insert(@RequestBody Users users)
    {

        System.out.println(users);
        return "insert-success";

    }
    //如果是json数组,直接转成list集合

    @PostMapping("/inserts")
    public  String inserts(@RequestBody List<Users> list)
    {

        System.out.println(list);
        return "inserts-success";

    }


}
